﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.News;
using ChannelVN.Cached;
using GUI.Extensions;

namespace GUI.Controls.Sport
{
    public partial class LatestNews : System.Web.UI.UserControl
    {
        public bool GetDataFromNoCacheLayer = false;

        private const string LiFormat = "<li><span></span><a title=\"{0}\" href=\"{1}\">{2}{3}</a></li>";
        private const int Top = 12;

        protected void Page_Load(object sender, EventArgs e)
        {
            var enableHtmlCached = GetDataFromNoCacheLayer && CacheSettingExtensions.EnableCacheHtml;
            var bo = enableHtmlCached ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var isCachedUpdating = enableHtmlCached && CacheController.IsCachedUpdating;

            var dt = bo.GetSportNewsByPosition(top: Top, typeId: DefinedConstants.SportNews.TypeIds.TinHot, getDataFromMasterDataBase: isCachedUpdating);
            Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new System.Text.StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    sb.Append(string.Format(
                            LiFormat,
                            r[SqlConstants.NewsPublish.EncodedTitle],
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title],
                            r[SqlConstants.NewsPublish.NewsTypeIcon]
                        ));
                }

                ltrList.Text = sb.ToString();
                return true;
            }
            return false;
        }
    }
}